package cloudContact;

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class Emailer {

	private Emailer() {

	}

	public static void sendEmail(String addressTo, String subject,
			String textMsg) {
		// Recipient's email ID needs to be mentioned.
		String to = addressTo;

		// Sender's email ID needs to be mentioned
		String from = "cloudctac@gmail.com";

		Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "465");
 
		Session session = Session.getDefaultInstance(props,
			new javax.mail.Authenticator() {
				protected PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication("cloudctac@gmail.com","CP@ssw0rd");
				}
			});

		try {
			// Create a default MimeMessage object.
			MimeMessage message = new MimeMessage(session);

			// Set From: header field of the header.
			message.setFrom(new InternetAddress(from));

			// Set To: header field of the header.
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(
					to));
		
			// Set Subject: header field
			message.setSubject(subject);
			
			message.setContent(textMsg, "text/html");

			// Send message
			Transport.send(message);

			System.out.println("Sent message successfully....");
		} catch (MessagingException mex) {
			System.out.println("Sent message failed");
			System.out.println("Sent message To " + addressTo);
			mex.printStackTrace();
		}
	}
}
